package gl.android.biyiqing.fragment;

import gl.android.biyiqing.FragmentContainerActivity;
import gl.android.biyiqing.R;
import gl.android.biyiqing.api.API;
import gl.common.AjaxCallBackProxy;
import gl.common.BaseControl;
import gl.common.UserManger;
import gl.uitls.Tip;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 消息盒子
 * 
 * @author geliang
 * 
 */
public class MyAccountFragment extends BaseListFragmet implements
		OnClickListener {

	public MyAccountFragment() {
	}

	private TextView mPost;
	private TextView mJinbi;
	private TextView mJifen;
	private TextView mKx;
	private TextView mSy;
	private TextView mSs;

	// End Of Content View Elements

	private void bindViews() {

		mPost = (TextView) findViewById(R.id.post);
		mJinbi = (TextView) findViewById(R.id.jinbi);
		mJifen = (TextView) findViewById(R.id.jf);
		mKx = (TextView) findViewById(R.id.kx);
		mSy = (TextView) findViewById(R.id.sy);
		mSs = (TextView) findViewById(R.id.ss);
		findViewById(R.id.tr_jinbi).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(),
						FragmentContainerActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(FragmentContainerActivity.BaseFragmentClassName,
						CostJinBiListFragment.class.getName());
				intent.putExtras(bundle);
				getActivity().startActivity(intent);
			}
		});
		findViewById(R.id.tr_post).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(),
						FragmentContainerActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(FragmentContainerActivity.BaseFragmentClassName,
						CostpostListFragment.class.getName());
				intent.putExtras(bundle);
				getActivity().startActivity(intent);
			}
		});
		findViewById(R.id.tr_jifen).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(),
						FragmentContainerActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(FragmentContainerActivity.BaseFragmentClassName,
						CostpointListFragment.class.getName());
				intent.putExtras(bundle);
				getActivity().startActivity(intent);
			}
		});
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.my_account, container, false);
		bindViews();
		initAccountInfo();
		return rootView;
	}

	public void initAccountInfo() {
		getCount("GetPostCount?userid=%s", mPost, "可用邮票:%s");
		getCount("GetMoneyCount?userid=%s", mJinbi, "可用金币:%s");
		getCount("GetPointsCount?userid=%s", mJifen, "签到积分:%s");
		getIsKaiTong("GetSMSInfo?userid=%s", mKx, "已开通(%s到%s)", "没有开通此服务");
		getIsKaiTong("GetDisplayInfo?userid=%s", mSy, "已开通(%s到%s)", "没有开通此服务");
		getIsKaiTong("GetRankInfo?userid=%s", mSs, "已开通(%s到%s)", "没有开通此服务");
	}

	void getCount(String url, final TextView tv, final String format) {
		BaseControl.get(API.URL + url, new AjaxCallBackProxy() {
			@Override
			public void onSuccess(String t) {
				super.onSuccess(t);
				try {
					tv.setText(String.format(format,
							new JSONObject(t).getString("data")));

				} catch (Exception e) {
					e.printStackTrace();
					onFailure(e, 0, e.getMessage());
				}
			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Tip.show("获取拥有财产数量失败");
			}
		}, new Object[] { UserManger.getInstance().userid });
	}

	void getIsKaiTong(String url, final TextView tv, final String yes,
			final String no) {
		BaseControl.get(API.URL + url, new AjaxCallBackProxy() {
			@Override
			public void onSuccess(String t) {
				super.onSuccess(t);
				try {
					if ("1".equals(new JSONObject(t).getString("msg"))) {
						
						JSONObject jo = new JSONObject(t).getJSONArray("data").optJSONObject(0);
						tv.setText(String.format(yes, jo.optString("starttime"),jo.optString("endtime")));
					}
					else {
						tv.setText(no);
					}
				} catch (Exception e) {
					e.printStackTrace();
					onFailure(e, 0, e.getMessage());
					tv.setText("未能查询到结果");
				}
			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Tip.show("未能查询到结果");
			}
		}, new Object[] { UserManger.getInstance().userid });
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}
}